package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.PlatformIntroduction;
import com.jyy.prefabricated.service.PlatformIntroductionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 平台介绍
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class PlatformIntroductionController {

    @Autowired
    private PlatformIntroductionService platformIntroductionService;

    /**
     * 新增介绍
     * @param platformIntroduction
     * @return
     */
    @PostMapping("/platform-introduction")
    public AjaxResult insert(@Valid @RequestBody PlatformIntroduction platformIntroduction){
        log.info("Rest request to insert platformIntroduction:{}",platformIntroduction);
        int id = platformIntroductionService.insert(platformIntroduction);
        PlatformIntroduction result = platformIntroductionService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 修改介绍
     * @param platformIntroduction
     * @return
     */
    @PutMapping("/platform-introduction")
    public AjaxResult update(@Valid @RequestBody PlatformIntroduction platformIntroduction){
        log.info("Rest request to update platformIntroduction:{}",platformIntroduction);
        platformIntroductionService.update(platformIntroduction);
        PlatformIntroduction result = platformIntroductionService.selectById(platformIntroduction.getId());
        return AjaxResult.success(result);
    }

    /**
     * 批量更新状态
     * @param dto
     * @return
     */
    @PutMapping("/platform-introduction-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto){
        log.info("Rest request to batch-update platformIntroduction:{}",dto);
        platformIntroductionService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    @DeleteMapping("/platform-introduction-batch")
    public AjaxResult deleteById(@RequestBody BatchDto dto){
        log.info("Rest request to batch-delete platformIntroduction by id:{}",dto);
        int row = platformIntroductionService.batchDelete(dto);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询介绍详情
     * @param id
     * @return
     */
    @GetMapping("/platform-introduction/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find platformIntroduction by id:{}",id);
        PlatformIntroduction result = platformIntroductionService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除介绍
     * @param id
     * @return
     */
    @DeleteMapping("/platform-introduction/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete platformIntroduction by id:{}",id);
        int row = platformIntroductionService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询上一条介绍
     * @param id
     * @return
     */
    @GetMapping("/platform-introduction-prev/{id}")
    public AjaxResult selectByPrev(@PathVariable Integer id){
        log.info("Rest request to find platformIntroduction by prev id:{}",id);
        List<PlatformIntroduction> result = platformIntroductionService.selectByPage(id,"prev");
        return AjaxResult.success(result);
    }

    /**
     * 查询下一条介绍
     * @param id
     * @return
     */
    @GetMapping("/platform-introduction-next/{id}")
    public AjaxResult selectByNext(@PathVariable Integer id){
        log.info("Rest request to find platformIntroduction by next id:{}",id);
        List<PlatformIntroduction> result = platformIntroductionService.selectByPage(id,"next");
        return AjaxResult.success(result);
    }

    /**
     * 分页查询介绍
     * @param categoryId 分类id
     * @param title 标题
     * @param status 状态
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/platform-introduction")
    public AjaxResult selectByNext(@RequestParam(required = false) Integer categoryId, @RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false) Integer status, Pageable pageable){
        log.info("Rest request to find platformIntroduction by params categoryId:{}, title:{}, status:{}",categoryId, title, status);
        //List<Integer> categoryIds = platformIntroductionService.selectDeepCategoryIds(categoryId);

        List<Integer> categoryIds = Lists.newArrayList();
        if (categoryId != null) {
            categoryIds.add(categoryId);
        }
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<PlatformIntroduction> platformIntroduction = platformIntroductionService.selectDeepByParams(categoryIds, title, status);
        PageInfo<PlatformIntroduction> result = new PageInfo<>(platformIntroduction);
        return AjaxResult.success(result);
    }

    /**
     * 查询介绍
     * @param categoryId 分类id
     * @param title 标题
     * @param status 状态
     * @return
     */
    @GetMapping("/platform-introduction-list")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer categoryId, @RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false) Integer status){
        log.info("Rest request to find platformIntroduction by params categoryId:{}, title:{}, status:{}",categoryId, title, status);
        List<Integer> categoryIds = platformIntroductionService.selectDeepCategoryIds(categoryId);
        List<PlatformIntroduction> result = platformIntroductionService.selectDeepByParams(categoryIds, title, status);
        return AjaxResult.success(result);
    }
}
